package com.kvest.testtask.activity;

import android.app.AlertDialog;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.kvest.testtask.android.SiteOverlay;
import com.kvest.testtask.R;
import com.kvest.testtask.core.SitesManager;
import com.kvest.testtask.datamodel.Site;

public class SiteMapActivity extends MapActivity implements SiteOverlay.TapListener
{
	public static final String SITE_ID_EXTRA = "site_id";
	
	private Site shownSite;
	
	private MapView map;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.site_map_layout);
        
        map = (MapView)findViewById(R.id.mv_map);
        map.setBuiltInZoomControls(true);
        
        Drawable marker = getResources().getDrawable(R.drawable.marker);
        marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
        
        SiteOverlay overlay = new SiteOverlay(marker);
        overlay.setTapListener(this);
        map.getOverlays().add(overlay);
        
        //find shown site object
        if (getIntent().hasExtra(SITE_ID_EXTRA))
        {
        	int siteId = getIntent().getIntExtra(SITE_ID_EXTRA, 0);
        	shownSite = SitesManager.getManager().getSite(siteId);
        	
        	//Create GEO point and item for overlay, add it to overlay
        	GeoPoint point = new GeoPoint((int)(shownSite.getPrimaryLatitude() * 1000000), 
        								  (int)(shownSite.getPrimaryLongitude() * 1000000));
        	overlay.addOverlayItem(new OverlayItem(point, shownSite.getName(), shownSite.getName()));
        	
        	
        	//set our point as central and set another parameters for map
            map.getController().setCenter(point);
            map.getController().setZoom(15);
        }
    }
	
	@Override
	protected boolean isRouteDisplayed() 
	{
		return false;
	}

	@Override
	public void tapAction(GeoPoint point) 
	{
		if (shownSite != null)
		{
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle(shownSite.getName());
			dialog.setMessage(getSiteAsText(shownSite));
			dialog.show();
		}
	}
	
	private String getSiteAsText(Site site)
	{
		Resources res = getResources();
		
		String result = "";
		result += res.getString(R.string.feature_id) +  " : " + site.getFeatureId() + "\n";
		result += res.getString(R.string.name) +  " : " + site.getName() + "\n";
		result += res.getString(R.string.county_name) +  " : " + site.getCountyName() + "\n";
		result += res.getString(R.string.description) +  " : " + site.getDescription() + "\n";
		result += res.getString(R.string.feat_class) +  " : " + site.getFeatClass() + "\n";
		result += res.getString(R.string.fips_class) +  " : " + site.getFipsClass() + "\n";
		result += res.getString(R.string.fips_county_cd) +  " : " + site.getFipsCountyCd() + "\n";
		result += res.getString(R.string.full_county_name) +  " : " + site.getFullCountyName() + "\n";
		result += res.getString(R.string.link_title) +  " : " + site.getLinkTitle() + "\n";
		result += res.getString(R.string.url) +  " : " + site.getUrl() + "\n";
		result += res.getString(R.string.latitude) +  " : " + site.getPrimaryLatitude() + "\n";
		result += res.getString(R.string.longitude) +  " : " + site.getPrimaryLongitude() + "\n";
		result += res.getString(R.string.state_abbreviation) +  " : " + site.getStateAbbreviation() + "\n";
		result += res.getString(R.string.state_name) +  " : " + site.getStateName() + "\n";

		return result;
	}

}
